﻿//#define TEST_SDK

#if (!UNITY_EDITOR || TEST_SDK) && WEIXINMINIGAME

using Cysharp.Threading.Tasks;
using WeChatWASM;

namespace KuiHuaBaoDian.SDK {

    public static partial class API {

        private partial class Implementation {

            public UniTask<IAPI.ISystemInfo> GetSystemInfo(string inviterKey = null) {
                IsEnabled = true;
                UnityEngine.Application.targetFrameRate = 60;

                var systemInfo = new SystemInfo();
                WX.GetSystemInfo(new GetSystemInfoOption { });
                WX.SetKeepScreenOn(new SetKeepScreenOnOption { keepScreenOn = true });
                WX.OnShow(result => {
                    WX.SetKeepScreenOn(new SetKeepScreenOnOption { keepScreenOn = true });
                    ApplicationShow?.Invoke(null);
                });
                WX.OnHide(result => {
                    ApplicationHide?.Invoke();
                });
                var launchOptions = WX.GetLaunchOptionsSync();
                if (launchOptions != null && !string.IsNullOrEmpty(inviterKey)) {
                    if (launchOptions.query.TryGetValue(inviterKey, out var inviter)) {
                        systemInfo.Inviter = inviter;
                    }
                }
                return UniTask.FromResult<IAPI.ISystemInfo>(systemInfo);
            }
        }
    }
}
#endif